﻿Imports System
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Region
Imports System.Drawing.Drawing2D
Imports Microsoft.Office.Interop

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Module UtilitairesDivers

#Region "Déclaration API"
    Public Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Int32, ByVal Y1 As Int32, ByVal X2 As Int32, ByVal Y2 As Int32) As IntPtr
    Public Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As IntPtr, ByVal hRgn As IntPtr, ByVal bRedraw As Boolean) As Int32

    Public Declare Function GetDC Lib "user32" (ByVal hwnd As IntPtr) As IntPtr
    Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long

    Public Declare Function GetFocus Lib "user32.dll" () As IntPtr

    Public Const HWND_DESKTOP As Long = 0
    Public Const LOGPIXELSX As Long = 88
    Public Const LOGPIXELSY As Long = 90
#End Region

#Region "Fonctions liées à ZedGraph"
    'Public Sub miseEnFormeGraph(ByRef GPane As ZedGraph.GraphPane, ByVal titre As String, _
    '                            ByVal titreX As String, ByVal titreY As String, _
    '                            ByVal taillePolice As Integer, Optional ByVal formatY As String = "###,##0 %")

    '    Dim titleFont As ZedGraph.FontSpec
    '    titleFont = New ZedGraph.FontSpec("Tahoma", taillePolice, Color.Black, True, False, False, Color.White, Nothing, ZedGraph.FillType.Brush)
    '    titleFont.Border.IsVisible = False

    '    Dim titleAxeXFont As ZedGraph.FontSpec
    '    titleAxeXFont = New ZedGraph.FontSpec("Tahoma", taillePolice, Color.Black, False, False, False, Color.White, Nothing, ZedGraph.FillType.Brush)
    '    titleAxeXFont.Border.IsVisible = False

    '    Dim titleAxeYFont As ZedGraph.FontSpec
    '    titleAxeYFont = New ZedGraph.FontSpec("Tahoma", taillePolice, Color.Black, False, False, False, Color.White, Nothing, ZedGraph.FillType.Brush)
    '    titleAxeYFont.Border.IsVisible = False
    '    titleAxeYFont.Angle = 180

    '    Dim scaleAxeYFont As ZedGraph.FontSpec
    '    scaleAxeYFont = New ZedGraph.FontSpec("Tahoma", taillePolice, Color.Black, False, False, False, Color.White, Nothing, ZedGraph.FillType.Brush)
    '    scaleAxeYFont.Border.IsVisible = False
    '    scaleAxeYFont.Angle = 90

    '    GPane.Title.Text = titre
    '    GPane.Title.FontSpec = titleFont

    '    GPane.XAxis.Title.Text = titreX
    '    GPane.XAxis.Title.FontSpec = titleAxeXFont
    '    GPane.XAxis.Scale.FontSpec = titleAxeXFont

    '    GPane.YAxis.Title.Text = titreY
    '    GPane.YAxis.Title.FontSpec = titleAxeYFont
    '    GPane.YAxis.Scale.FontSpec = scaleAxeYFont
    '    GPane.YAxis.Scale.Format = formatY

    '    GPane.XAxis.MajorGrid.IsVisible = True
    '    GPane.YAxis.MajorGrid.IsVisible = True
    '    GPane.XAxis.MajorGrid.Color = Color.Gray
    '    GPane.YAxis.MajorGrid.Color = Color.Gray

    '    GPane.Legend.IsVisible = False


    'End Sub
#End Region

#Region "Mise à jour des dgv/treeView/listView"
    Public Sub MAJdgv(ByVal dgv As DataGridView, ByVal typeRef As String)

        'Primes DC
        If dgv.Columns.Contains(nomPComDC_Derog) Then
            dgv.Columns(nomPComDC_Derog).DefaultCellStyle.Format = "###,##0.000 %"
            dgv.Columns(nomPComDC_Derog).HeaderText = descPComDC_Derog
        End If
        If dgv.Columns.Contains(nomPComDC_Non_Derog) Then
            dgv.Columns(nomPComDC_Non_Derog).DefaultCellStyle.Format = "###,##0.000 %"
            dgv.Columns(nomPComDC_Non_Derog).HeaderText = descPComDC_Non_Derog
        End If
        If dgv.Columns.Contains(nomPourDC_Derog) Then
            dgv.Columns(nomPourDC_Derog).DefaultCellStyle.Format = "###,##0 %"
            dgv.Columns(nomPourDC_Derog).HeaderText = descPourDC_Derog
        End If

        'Primes INCAP/INVAL
        If dgv.Columns.Contains(nomPComINCINV_Derog) Then
            dgv.Columns(nomPComINCINV_Derog).DefaultCellStyle.Format = "###,##0.000 %"
            dgv.Columns(nomPComINCINV_Derog).HeaderText = descPComINCINV_Derog
        End If
        If dgv.Columns.Contains(nomPComINCINV_Non_Derog) Then
            dgv.Columns(nomPComINCINV_Non_Derog).DefaultCellStyle.Format = "###,##0.000 %"
            dgv.Columns(nomPComINCINV_Non_Derog).HeaderText = descPComINCINV_Non_Derog
        End If
        If dgv.Columns.Contains(nomPourINCINV_Derog) Then
            dgv.Columns(nomPourINCINV_Derog).HeaderText = descPourINCINV_Derog
            dgv.Columns(nomPourINCINV_Derog).DefaultCellStyle.Format = "###,##0 %"
        End If

        'Taux de mortalité
        If dgv.Columns.Contains(nomPourINCINV_Derog) Then
            dgv.Columns(nomPourINCINV_Derog).HeaderText = descPourINCINV_Derog
            dgv.Columns(nomPourINCINV_Derog).DefaultCellStyle.Format = "###,##0 %"
        End If
        If dgv.Columns.Contains(nomPourINCINV_Derog) Then
            dgv.Columns(nomPourINCINV_Derog).HeaderText = descPourINCINV_Derog
            dgv.Columns(nomPourINCINV_Derog).DefaultCellStyle.Format = "###,##0 %"
        End If

        If dgv.Columns.Contains(nomAgeAnEntier) Then
            dgv.Columns(nomAgeAnEntier).HeaderText = descAgeAnEntier
        End If
        If dgv.Columns.Contains(nomTxMortAn) Then
            dgv.Columns(nomTxMortAn).HeaderText = descTxMortAn
            dgv.Columns(nomTxMortAn).DefaultCellStyle.Format = "###,##0.0000 %"
        End If

        If typeRef = "INCINVtaux" Then
            dgv.Columns(0).HeaderText = "Age vs N° Mois"
            For i = 1 To dgv.Columns.Count - 1
                dgv.Columns(i).HeaderText = CStr(i + 17) & " ans"
                dgv.Columns(i).DefaultCellStyle.Format = "###,###0.00"
            Next
        End If
        If typeRef = "INCINVfra" Then
            dgv.Columns(0).HeaderText = "Age vs nb Mois"
            For i = 1 To dgv.Columns.Count - 1
                dgv.Columns(i).HeaderText = CStr(i + 17) & " ans"
                dgv.Columns(i).DefaultCellStyle.Format = "###,###0.00"
            Next
        End If
    End Sub
    'Fonction permettant de dérouler la treeview transmise en paramètre
    Public Sub deroule_treeView(ByRef trv_aDerouler As TreeView)
        Dim i As Integer
        For i = 0 To trv_aDerouler.Nodes.Count - 1
            trv_aDerouler.Nodes(i).ExpandAll()
        Next
    End Sub
    Public Sub remplit_lstV_from_ddtTable(ByRef l As ListView, ByRef d As DataTable)
        Dim it As ListViewItem
        For Each r As DataRow In d.Rows
            it = New ListViewItem()
            it.Font = New Font("Segoe UI", 9, FontStyle.Regular)
            If Not r.IsNull(0) Then
                it.SubItems(0).Text = r.Item(0)
                ''Else
                ''it.SubItems(0).Text = "Null"
                ''End If
                l.Items.Add(it)
            End If
        Next
    End Sub
#End Region

#Region "Export Excel"
    Public Sub ExportExcel(ByVal tble As DataTable)

        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet

        xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
        xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
        xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)

        'Ici on compte le nombre de lignes et de colonnes du dataTable
        Dim nbreLigne As Integer = tble.Rows.Count
        Dim nbreColon As Integer = tble.Columns.Count
        Dim x, y As Integer

        'On met la première ligne du fichier en gras
        xlSheet.Rows(1).Font.Bold = True

        For x = 0 To nbreColon - 1
            'Ici on prend le titre des colonnes du dataTable
            xlSheet.Cells(1, x + 1) = tble.Columns(x).ColumnName
            For y = 0 To nbreLigne - 1
                'Pour chaque colonne et pour chaque ligne on transfère les données
                xlSheet.Cells(y + 2, x + 1) = tble.Rows(y)(x)
            Next
        Next

        xlSheet.Application.Visible = True

    End Sub
#End Region

#Region "Méthodes de dessin"
    Public Sub color_degrade(ByRef ofrm As Windows.Forms.Control)
        Dim hwnd As IntPtr
        Dim hDC As IntPtr
        hwnd = ofrm.Handle
        Dim r1 As Rectangle = New Rectangle(1, 1, ofrm.Width - 1, ofrm.Height - 1)
        hDC = GetDC(hwnd)
        Dim lgb As LinearGradientBrush = New LinearGradientBrush(r1, Color.Red, Color.Blue, LinearGradientMode.Horizontal)
        'Récupération de la zone graphique associée au device context
        Dim gfx As Graphics = Graphics.FromHdc(hDC)
        gfx.FillRectangle(lgb, r1)
    End Sub
    Public Sub DrawBorder(ByVal arGfx As Graphics, ByVal arRC As Rectangle, _
         ByVal arcColor As Color)

        Dim lpPen As Pen = New Pen(arcColor, 1)

        arGfx.DrawLine(lpPen, arRC.X, arRC.Y + arRC.Height - 1, arRC.X, arRC.Y)
        arGfx.DrawLine(lpPen, arRC.X, arRC.Y, arRC.X + arRC.Width - 1, arRC.Y)
        If Not (arRC.Width = 0) Then
            arGfx.DrawLine(lpPen, arRC.X + arRC.Width - 1, arRC.Y, arRC.X + arRC.Width - 1, arRC.Top + arRC.Height - 1)
            arGfx.DrawLine(lpPen, arRC.X + arRC.Width - 1, arRC.Top + arRC.Height - 1, arRC.X, arRC.Top + arRC.Height - 1)
            lpPen.Dispose()
        End If
    End Sub
    Public Function TwipsPerPixelX() As Single
        Dim lngDC As Long
        lngDC = GetDC(HWND_DESKTOP)
        TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
        ReleaseDC(HWND_DESKTOP, lngDC)
    End Function
    Public Function TwipsPerPixelY() As Single
        Dim lngDC As Long
        lngDC = GetDC(HWND_DESKTOP)
        TwipsPerPixelY = 1440& / GetDeviceCaps(lngDC, LOGPIXELSY)
        ReleaseDC(HWND_DESKTOP, lngDC)
    End Function
    Sub CercleMoi(ByRef obj As Form)

        Dim Cercle As Long
        Dim H As Long
        Dim L As Long
        Dim HduBord As Long
        Dim LduBord As Long

        H = obj.Height
        L = obj.Width

        HduBord = Int(H / 100)
        LduBord = Int(L / 100)

        Cercle = CreateEllipticRgn(0, 0, L, H)
        SetWindowRgn(obj.Handle, Cercle, True)

    End Sub
    Sub EllipseMoi(ByVal obbj As Button)

        Dim Cercle As Long
        Dim H As Long
        Dim L As Long
        Dim HduBord As Long
        Dim LduBord As Long

        H = obbj.Height / TwipsPerPixelY()
        L = obbj.Width / TwipsPerPixelX()

        HduBord = Int(H / 100)
        LduBord = Int(L / 100)

        Cercle = CreateEllipticRgn(LduBord, HduBord, L - LduBord, H - HduBord)

        SetWindowRgn(obbj.Handle, Cercle, True)
    End Sub
    'Procédure permettant de récupérer la taille d'un texte en pixel
    'measureText(Txt_BaseEncours.Text, "Verdana", 9.75, txt_width, txt_height)
    'MsgBox("Largeur : " & txt_width & " Hauteur : " & txt_height)
    Public Sub measureText(ByVal bannerText As String, ByVal fontName As String, ByVal fontSize As Single, _
                       ByRef width As Single, ByRef Height As Single)
        Dim b As Bitmap
        Dim g As Graphics
        Dim f As New Font(fontName, fontSize)
        b = New Bitmap(1, 1, PixelFormat.Format32bppArgb)
        g = Graphics.FromImage(b)
        Dim stringSize As SizeF = g.MeasureString(bannerText, f)
        width = stringSize.Width
        Height = stringSize.Height
        g.Dispose()
        b.Dispose()
    End Sub
#End Region

End Module
